package com.tech.process

import com.tech.common.KuduUtil
import org.json4s.DefaultFormats
import org.json4s.jackson.JsonMethods

import java.text.SimpleDateFormat
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

object EventProcess {
  //  println("进入retaileventProcess方法")

  implicit val f: DefaultFormats.type = org.json4s.DefaultFormats

  val customer_id = "customer_id"

  def retaileventProcess(value: String, retaileventSchemaMap: mutable.HashMap[String, String], ruleMap: mutable.HashMap[String, mutable.HashMap[String, String]], customerSchemaMap: mutable.HashMap[String, String]): Unit = {

    val tmpMap = JsonMethods.parse(value).extract[Map[String, Any]]

    val retaileventMap = mutable.HashMap[String, Any]()

    //将数据装载到可变的map中
    tmpMap.foreach(i => retaileventMap.put(i._1, i._2))

    if (retaileventMap.contains(customer_id) && retaileventMap(customer_id) != null && retaileventMap(customer_id) != "") {
      retaileventMap.put("is_member", "1")
    } else {
      // FIXME 此处若有不包含detail_id脏数据会导致程序报错
      retaileventMap.put(customer_id, retaileventMap("detail_id"))
      retaileventMap.put("is_member", "0")
    }
    retaileventMap.put("event_month", new SimpleDateFormat("YYYYMM").format(retaileventMap("event_time")))
    retaileventMap.put("created_time", System.currentTimeMillis())
    retaileventMap.put("posted_time", System.currentTimeMillis())

    //    println("retailevent：开始写入kudu")
    KuduUtil.insertRow("retailevent", retaileventMap, retaileventSchemaMap)
    //    println("retailevent：写入kudu成功 ==> " + retaileventMap("customer_id") + "  " + retaileventMap("project"))

    val eventKey = retaileventMap("event_key").toString
    val project = retaileventMap("project").toString
    val projectEventKey = project + "|" + eventKey
    val customerId = retaileventMap("customer_id").toString

    println(projectEventKey)

    //按规则查询customer后写入
    if (ruleMap != null && ruleMap.contains(projectEventKey)) {

      val httpRuleMap = ruleMap(projectEventKey)

      if (httpRuleMap.nonEmpty) {
        val hashMap = JsonMethods.parse(value).extract[Map[String, Any]]

        val array = ArrayBuffer[String]("created_time")
        httpRuleMap.foreach(i => array.append(i._1))
        //查询customer
        val map1 = KuduUtil.getCustomerDetail(project, customerId, array)
        if (map1.get("created_time") != null) {
          httpRuleMap.foreach(
            t => {
              if (hashMap.contains(t._1) && hashMap(t._1) != null) {
                //                println("retailevent事件开始写入customer")
                t._2 match {
                  case "replace" =>
                    map1.put(t._1, hashMap(t._1))
                  case "addup" =>
                    map1(t._1).getClass.getSimpleName match {
                      case "BigInt" | "Long" => map1.put(t._1, ruleMap(t._1).toString.toLong + hashMap(t._1).toString.toLong)
                      case "Double" | "Integer" => map1.put(t._1, ruleMap(t._1).toString.toDouble + hashMap(t._1).toString.toLong)
                      case _ =>
                    }
                }
              }
            }
          )
          val createdTime: Long = try {
            map1.get("created_time").toString.toLong
          } catch {
            case e: Exception => System.currentTimeMillis()
          }
          KuduUtil.upsertCustomer(project, customerId, createdTime, map1, customerSchemaMap)
          println("retailevent：插入customer成功 ==> " + customerId + "  " + project)
        }

        println("完成处理")

      }
    }


  }


  def customerProcess(value: String, customerSchemaMap: mutable.HashMap[String, String]): Unit = {


    implicit val f: DefaultFormats.type = org.json4s.DefaultFormats
    val tmpMap = JsonMethods.parse(value).extract[Map[String, Any]]

    val customerMap = mutable.HashMap[String, Any]()



    //将数据装载到可变的map中
    tmpMap.foreach(i => customerMap.put(i._1, i._2))

    if (customerMap.contains(customer_id) && customerMap(customer_id) != null && customerMap(customer_id) != "") {
      customerMap.put("is_member", "1")
    } else {
      // FIXME 此处若有不包含detail_id脏数据会导致程序报错
      customerMap.put(customer_id, customerMap("detail_id"))
      customerMap.put("is_member", "0")
    }

    customerMap.put("created_time", System.currentTimeMillis())

    KuduUtil.insertRow("customer", customerMap, customerSchemaMap)
    //    println("customer：写入kudu成功 ==> " + customerMap("customer_id") + "  " + customerMap("project"))
  }

}
